package leetcode.problems;


import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gmwang on 2018/7/25
 */
public class _0729SelfDividingNumbers {
    /**
     *
     * A self-dividing number is a number that is divisible by every digit it contains.
     * 一个自划分数是一个数字，是由它所包含的每个数字整除。
     *
     * For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.
     * 例如，128是自分割数，因为128 % 1 == 0，128 % 2 == 0和128 % 8 == 0。
     * Also, a self-dividing number is not allowed to contain the digit zero.
     * 此外，不允许自分割数包含数字零。
     * Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.
     * 给定下限和上限数字，输出每个可能的自分割数的列表，包括边界。
     * Example 1:
     * Input:
     * left = 1, right = 22
     * Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
     * Note:
     *
     * The boundaries of each input argument are 1 <= left <= right <= 10000.
     */
    /**
     * 先比较某个值之前都是升序，再比较某个值之后都是降序
     * @param left,right
     * @return
     */
    public List<Integer> selfDividingNumbers(int left, int right) {
        List<Integer> list = new ArrayList<>();
        for(int i = left;i <= right;i ++){
            if((i+"").contains("0")) continue;
            if(isSelfDividingNumber(i)){
                list.add(i);
            }
        }
        return list;
    }

    /**
     * 判断是否是自分割数
     * @param selfDividingNumber
     * @return
     */
    public boolean isSelfDividingNumber(int selfDividingNumber){
        int flag = 0;
        String selfNumber = selfDividingNumber+"";
        for(int x = 0; x < selfNumber.length();x++){
            //每一位数都除尽
            if(selfDividingNumber % Integer.parseInt(selfNumber.substring(x,x+1)) == 0) flag++;
        }
        return flag==(selfDividingNumber+"").length()?true:false;
    }
    @Test
    public void test() {
        int left =1 ,right =22;
        List<Integer> res = selfDividingNumbers(left,right);
        System.out.println(res);
//        System.out.println((2+"").length());
    }
}
